package com.burgeon.backend.Rest.view.data;
/**
 * 主页视图一
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class MainViewData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public MainViewData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        ArrayList data = new ArrayList();

        JSONArray jsonArray = null;

        String str = RESULT(Data(), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        for (int i = 0; i < jsonArray.length(); i++) {
            HashMap map = new HashMap();
            map.put("date", jsonArray.getJSONArray(i).getString(0));
            map.put("qty", jsonArray.getJSONArray(i).getString(1));
            map.put("rate", (Double.parseDouble(jsonArray.getJSONArray(i).getString(2)) + "").replace(".0", "").replace("0.","").trim());
            data.add(map);
        }

        return data;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("BILLDATE");//时间
        columns.add("TOT_VIP_QTY");//昨日新增
        columns.add("VIP_RATE");//昨日会员率


        params.put("columns", columns);
        params.put("table", "APP_VIPNRTREND");//表名
        HashMap orderby = new HashMap();


        orderby.put("column", "BILLDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        MainViewData g = new MainViewData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
